package com;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import org.apache.commons.lang3.StringUtils;

import com.database.DataBaseService;
import com.util.PropUtil;
import com.window.TableListGrid;

public class AppStart extends JPanel {

	/**
	 * 程序入口
	 */
	public static void main(String[] args) {

		// JFrame.setDefaultLookAndFeelDecorated(true);// 将组建外观设置为Java外观
		JFrame frame = new JFrame();

		frame.setLayout(null);
		frame.setContentPane(new AppStart());

		frame.setSize(500, 500);
		frame.setVisible(true);
		frame.setTitle("新建连接");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		new AppStart().setWindowIcon(frame);
		frame.setLocationRelativeTo(null);
		// new TabComponentsDemo().runTest();

	}

	/**
	 * 
	 */
	JPanel jpanelFirst;
	JTextField hostIptxt;
	JTextField porttxt;
	JTextField dbnametxt;
	JTextField usernametxt;
	JTextField pwdtxt;

	public AppStart() {
		layoutComponents();
	}

	private void layoutComponents() {

		// 第一个标签下的JPanel
		jpanelFirst = new JPanel();
		jpanelFirst.setLayout(new GridLayout(0, 2, 5, 5));
		jpanelFirst.setSize(400, 400);
		jpanelFirst.setBorder(new LineBorder(Color.black));

		hostIptxt = addItem("主机名称或IP地址：", "hostip", "192.168.0.14");
		porttxt = addItem("端口：", "port", "3306");
		dbnametxt = addItem("数据库名称：", "dbname", "dev_datamanagercenter_db");
		usernametxt = addItem("用户名：", "username", "root");
		pwdtxt = addItem("密码：", "pwd", "123456");

		add(jpanelFirst);

		JPanel jpanel = new JPanel();
		JButton tryconnbutton = new JButton("连接测试");

		tryconnbutton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String uName = usernametxt.getText();
				String uPwd = pwdtxt.getText();
				String dbName = dbnametxt.getText();
				String port = porttxt.getText();
				String hostIp = hostIptxt.getText();
				String url = getMysqlDbUrl(dbName, port, hostIp);

				if (StringUtils.isEmpty(hostIp)) {
					showDialog("主机或IP不能为空！");
					return;
				}

				if (StringUtils.isEmpty(port)) {
					showDialog("端口不能为空！");
					return;
				}

				if (StringUtils.isEmpty(dbName)) {
					showDialog("数据库名称不能为空！");
					return;
				}

				if (StringUtils.isEmpty(uName)) {
					showDialog("用户名不能为空！");
					return;
				}

				if (StringUtils.isEmpty(uPwd)) {
					showDialog("密码不能为空！");
					return;
				}

				boolean result = DataBaseService
						.tryConnection(url, uName, uPwd);

				if (result) {
					showDialog("连接成功！");
				} else {
					showDialog("连接失败！");
				}
			}


		});

		JButton confirmbutton = new JButton("保存");

		confirmbutton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

				saveConnInfo();

				showDialog("保存成功");
			}
		});

		JButton viewtablebutton = new JButton("查看数据库表");
		viewtablebutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				saveConnInfo();
				TableListGrid frame = new TableListGrid();
				frame.setVisible(true);
			}
		});

		jpanel.add(tryconnbutton);
		jpanel.add(confirmbutton);
		jpanel.add(viewtablebutton);

		add(jpanel);

	}
	
	private void saveConnInfo() {
		String uName = usernametxt.getText();
		String uPwd = pwdtxt.getText();
		String dbName = dbnametxt.getText();
		String port = porttxt.getText();
		String hostIp = hostIptxt.getText();
		
//		String url = "jdbc:mysql://" + hostIp + ":" + port + "/"
//				+ dbName + "?useUnicode=true&characterEncoding=UTF-8&connectTimeout=3000&socketTimeout=60000";
		String url = getMysqlDbUrl(dbName, port, hostIp);

		PropUtil.writeDbUrl(url);
		PropUtil.writeUname(uName);
		PropUtil.writeUpwd(uPwd);
	}
	
	private String getSqlServerDbUrl(String dbName, String port, String hostIp) {
		String url = "jdbc:sqlserver://" + hostIp + ":" + port + ";DatabaseName="+ dbName;
		return url;
	}
	
	private String getMysqlDbUrl(String dbName, String port, String hostIp) {
		String url = "jdbc:mysql://" + hostIp + ":" + port + "/"
				+ dbName + "?useUnicode=true&characterEncoding=UTF-8&connectTimeout=3000&socketTimeout=60000";
		return url;
	}

	private JTextField addItem(String labeltxt, String txtname,
			String defaulttxt) {
		JLabel label = new JLabel(labeltxt);

		JTextField txt = new JTextField();
		txt.setColumns(20);
		txt.setName(txtname);
		txt.setText(defaulttxt);

		jpanelFirst.add(label);
		jpanelFirst.add(txt);
		return txt;
	}

	private void showDialog(String tip) {
		JOptionPane.showMessageDialog(null, tip, tip,
				JOptionPane.INFORMATION_MESSAGE);
	}

	private JTextField addItem(String labeltxt, String txtname) {
		return addItem(labeltxt, txtname, null);
	}

	private ImageIcon createImageIcon(String path) {

		URL url = AppStart.class.getResource(path);
		if (url == null) {
			System.out.println("the image " + path + " is not exist!");
			return null;
		}
		return new ImageIcon(url);
	}

	// 设置窗口图标
	protected void setWindowIcon(JFrame frame) {
		ImageIcon imageIcon = new ImageIcon(getClass().getResource(
				"/images/link.jpg"));
		// 设置标题栏的图标为face.gif
		frame.setIconImage(imageIcon.getImage());
	}

	private static void saveConnInfo(JButton confirmbutton) {
		confirmbutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

			}
		});
	}

}
